package st.magnuzon.book.client.console;

import st.magnuzon.book.client.custom.ui.ConsoleLinkWidget;
import st.magnuzon.book.shared.gae.GaeUserServiceRequest;
import st.magnuzon.book.shared.gae.MakesGaeRequests;

import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.Window;

public class ConsoleLinkWidgetDriver {
	private final MakesGaeRequests requests;

    public ConsoleLinkWidgetDriver(MakesGaeRequests requests) {
        this.requests = requests;
    }
 
    public void setWidget(final ConsoleLinkWidget widget) {
        GaeUserServiceRequest request = requests.userServiceRequest();

        request.isUserAdmin().to(new Receiver<Boolean>() {
            public void onSuccess(Boolean response) {
                if (response.booleanValue() == true) {
                    UrlBuilder urlBuilder = Window.Location.createUrlBuilder();
                    urlBuilder.setPath("/admin/console.html");
                    widget.setConsoleUrl(urlBuilder.buildString());
                    widget.setVisible(true);
                }
                else {
                    widget.setVisible(false);
                }
            }
        });
        request.fire();
    }
}
